package com.zsh.jedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

/**
 * @author zsh
 * @Description
 * @create 2022-04-21 10:08
 */
public class JedisDemo1 {
    public static void main(String[] args) {
        //创建Jedis对象
        Jedis jedis = new Jedis("192.168.200.128",6379);

        //测试
        String ping = jedis.ping();
        System.out.println(ping);
    }

    @Test
    public void demo1(){
        Jedis jedis = new Jedis("192.168.200.128",6379);

        Set<String> keys = jedis.keys("*");
        for(String key : keys){
            System.out.println(key);
        }
        //添加
//        jedis.set("k1","v1");

        //获取
//        System.out.println(jedis.get("k1"));

        //设置多个key 和 value
        jedis.mset("k1","v1","k2","v2");
        List<String> mget = jedis.mget("k1", "k2");
        System.out.println(mget);


    }


    //list
    @Test
    public void demo2(){
        Jedis jedis = new Jedis("192.168.200.128",6379);
        jedis.lpush("key1","lucy","mary","jack");
        List<String> key1 = jedis.lrange("key1", 0, -1);
        System.out.println(key1);
    }

    //set
    @Test
    public void demo3(){
        Jedis jedis = new Jedis("192.168.200.128",6379);
        jedis.sadd("key2","lucy","jack");

        Set<String> key2 = jedis.smembers("key2");
        System.out.println(key2);
    }

    //hash
    @Test
    public void demo4(){
        Jedis jedis = new Jedis("192.168.200.128",6379);
        jedis.hset("users","age","20");
        String hget = jedis.hget("users", "age");

        System.out.println(hget);
    }

    //zset
    @Test
    public void demo5(){
        Jedis jedis = new Jedis("192.168.200.128",6379);
        jedis.zadd("china1",100d,"shanghai1");
        jedis.zadd("china1",200d,"shanghai2");
        jedis.zadd("china1",300d,"shanghai1");

        Set<String> china = jedis.zrange("china1", 0, -1);
        System.out.println(china);
    }

}
